// pagesLive/produst/produstAdd.js
var siteinfo = require("../../siteinfo.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop_produstList: [],
    checkboxAllDisable: false,
    produst_id: [],
    checkedCount: 0,
    checked:false,
    checkedAll:false,
    goods_detail_pages: '/pagesPluginShop/goods_detail/goods_detail'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let base_info = wx.getStorageSync('global_baseinfo')
    console.log(base_info.base_color)
    wx.setNavigationBarColor({
      backgroundColor: base_info.base_color,
      frontColor: '#000000',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    this.getMerchantProdust();

      wx.hideLoading({
        complete: (res) => {},
      })

    setTimeout(() => {
      wx.showToast({
        title: '载入成功',
        icon: 'success',
        duration: 2000
      })
    }, 1600);
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  // showLoad: function(e){
 
  // },

//checkbox选中事件
  checkboxChange:  function (e) {
    console.log('点击选中了')
     var that = e
    // console.log(that)
    var key = that.currentTarget.dataset.indexid;
    console.log('索引',key)
  
    this.setData({
      checkedAll: false
    })
    let checkedCount = this.data.checkedCount
    if (that.detail.value != ""){
      this.data.produst_id.push(that.detail.value)
      checkedCount++
      this.setData({
        checkedCount: checkedCount
      })
     
    }else{
      this.data.produst_id.splice(key);
      console.log('索引',key)
      console.log(false)
      checkedCount--
      this.setData({
        checkedCount: checkedCount
      })
      console.log(this.data.produst_id)
    }
 

  },
  //全选
  checkedAll:function (e) {

  let checkedCount = this.data.checkedCount
    console.log(e.detail.value)
    if (e.detail.value.length > 0){
      //全选
      this.data.shop_produstList.forEach((item, index)=>{
       item.checked = true
        console.log('item',item)
        if(item.is_live === 0)(
        this.data.produst_id.push(item.id),
            checkedCount = this.data.shop_produstList.length
       )
        if (item.is_live > 0){
          wx.showModal({
            title: '提示',
            content:'请删除对应的直播商品！',
            showCancel: false,
          })
          this.setData({
            checkedAll: false
          })
          checkedCount = 0
        }

      });

      //取出商品id
      // let productId = this.detailx;
      console.log(this.data);
    }else{
      //取消全选
      this.data.shop_produstList.forEach((item, index)=>{
        item.checked = false
      })
     this.setData({
        produst_id: []
      });
      //清空数量
      checkedCount = 0;
    }
    // console.log(this.data.shop_produstList)
    //设置数据
    this.setData({
      shop_produstList:this.data.shop_produstList,
      checkedCount: checkedCount
    })
  },
  //获取商家商品
  getMerchantProdust:function(e){
    var suid = wx.getStorageSync('suid');
    var status = 1
    var uniacid = siteinfo.uniacid
    wx.request({
      url:  siteinfo.site_url + "api/live/getMerchantProdust",
      data: {
        suid: suid,
        status: status,
        uniacid: uniacid
      },
      success: (result) => {
        console.log(result.data.data)
        this.setData({
          shop_produstList: result.data.data
        })
      },
    })
  },
  	// 进入商品详情页面
	doProdustInfo: function(e) {
		var that = e;
		var url = that.currentTarget.dataset.url;
		console.log(url);
		wx.navigateTo({
			url: url
		})
  },
  
  // 添加商品
  saveProdust:function(e){
    wx.showLoading({
      title: '提交中'
    })
    if(this.data.produst_id == ""){
      wx.hideLoading({
        complete: (res) => {},
      })
      wx.showModal({
          title: '提示',
          content:'请选择商品',
          showCancel: false
      })
      return false
    }

    let produstId = this.data.produst_id
    wx.request({
      url: siteinfo.site_url + "api/live/addRoomProdust",
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      dataType: 'json',
      data: {
        produstid:produstId,
        uniacid: siteinfo.uniacid,
        suid:wx.getStorageSync('suid')
      },
      success: function(res){
        console.log(res.data);
        let data = res.data;
        if (data.errcode === 0){
          wx.hideLoading({
            complete: (res) => {},
          });
          wx.showModal({
            title: '提示',
            content:'提交成功！',
            showCancel: false,
            success (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url:'/pagesLive/produst/produstList'
                })
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }else if(data.errcode === 300018){
          wx.hideLoading({
            complete: (res) => {},
          });
          wx.showModal({
            title:'提示',
            content: '商品图片宽度和高度不能超过300px'
          })
        }else if(data.errcode === 300007){
          wx.hideLoading({
            complete: (res) => {},
          });
          wx.showModal({
            title:'提示',
            content: '商品链接错误'
          })
        }
      }
    })
  }

})